package collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * Collection的使用
 */
public class Demo02 {
    public static void main(String[] args) {
        //新建一个Collection对象
        Collection collection = new ArrayList<>();
        //1.添加数据
        Student s1 = new Student("curry", 34);
        collection.add(s1);
        collection.add(new Student("张三", 18));
        collection.add(new Student("李四", 20));
        collection.add(new Student("王五", 33));
        System.out.println("元素个数" + collection.size());
        System.out.println(collection.toString());
        //2.删除
        collection.remove(s1);
        //怎么删除王五
 //       collection.clear();
        System.out.println("元素个数" + collection.size());
        //3.遍历
        // 3.1增强For循环
        for (Object o : collection) {
            Student s = (Student) o;
            System.out.println(s);
        }
        //3.2 迭代器: hasNext() next() remove(); 迭代器过程中不能使用collection的remove()方法
        Iterator it = collection.iterator();
        while (it.hasNext()) {
            Student s = (Student) it.next();
            if ("王五".equals(s.getName()) ) {
              it.remove();
            }
        }
        for (Object o : collection) {
            Student s = (Student) o;
            System.out.println(s);
        }
   //     System.out.println(collection.size());
        //判断
        System.out.println(collection.contains(new Student("curry", 34)));
        System.out.println(s1);
    }
}
